package uk.ac.nott.cs.g53dia;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.text.DefaultEditorKit.BeepAction;

import uk.ac.nott.cs.g53dia.bee.DemoSimulator;
/**
 * A simple user interface for watching an individual bee.
 *
 * @author Neil Madden.
 */
/*
 * Copyright (c) 2003 Stuart Reeves
 * Copyright (c) 2003-2005 Neil Madden (nem@cs.nott.ac.uk).
 * 
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
public class BeeViewer extends JFrame {
    BeeViewerIconFactory iconfactory;
    JLabel[][] cells, bees;
    JLabel tstep, energy, pos, eggs;
    JButton pause;
    Bee bee;
    final static int SIZE = Bee.VIEW_RANGE * 2 + 1;

    public BeeViewer(Bee bee) {
    	this(bee, new DefaultBeeViewerIconFactory()); 
    }
    public BeeViewer(Bee bee, BeeViewerIconFactory fac) {
        this.bee = bee;
        this.iconfactory = fac;
        Container c     = getContentPane();
        c.setLayout(new BorderLayout());

        // Create the cell viewer
        cells           = new JLabel[SIZE][SIZE];
        bees            = new JLabel[SIZE][SIZE];
        JLayeredPane lp = new JLayeredPane();
        JPanel p        = new JPanel(new GridLayout(SIZE, SIZE));
        JPanel beePanel = new JPanel(new GridLayout(SIZE, SIZE));
        p.setBackground(new Color(190,225,190));

        for (int y = 0; y < SIZE; y++) {
            for (int x = 0; x < SIZE; x++) {
            	bees[x][y] = new JLabel();
                cells[x][y] = new JLabel();
                beePanel.add(bees[x][y]);
                p.add(cells[x][y]);
            }
        }
        lp.add(p,new Integer(0));
        beePanel.setOpaque(false);
        lp.add(beePanel,new Integer(1));
        p.setBounds(0,0,600,600);
        beePanel.setBounds(0,0,600,600);
        // Create a bee label
        JLabel beelabel = new JLabel(iconfactory.getIconForBee(bee));
        lp.add(beelabel,new Integer(1)); // Add above the background
        lp.setSize(new Dimension(600,600));
        beelabel.setBounds(275,275,40,40);
        c.add(lp, BorderLayout.CENTER);

        // Create some labels to show info about the bee and environment
        JPanel infop    = new JPanel(new GridLayout(2,4));
        infop.add(new JLabel("Timestep:"));
        tstep = new JLabel("0");
        infop.add(tstep);
        infop.add(new JLabel("Energy:"));
        energy = new JLabel("200");
        infop.add(energy);
        infop.add(new JLabel("Position:"));
        pos = new JLabel("(0,0)");
        infop.add(pos);
        infop.add(new JLabel("Eggs Laid:"));
        eggs = new JLabel("0");
        infop.add(eggs);
        pause = new JButton("Pause");
        pause.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DemoSimulator.pause();
			}
        });
        infop.add(pause);

        c.add(infop, BorderLayout.SOUTH);
        //infop.setPreferredSize(new Dimension(200,100));
        
        setSize(600,650);
        setTitle("Bee Viewer");
        setVisible(true);
    }

    public void setBee(Bee bee) {
        this.bee = bee;
    }

    public void tick(Environment env) {
        Cell[][] view = env.getView(bee.getPosition(),Bee.VIEW_RANGE);
        pos.setText(bee.getPosition().toString());
        tstep.setText(new String(""+env.getTimestep()));
        eggs.setText(new String(""+bee.eggs_laid));
        energy.setText(new String(""+bee.getEnergy()));
        for (int x = 0; x < SIZE; x++) {
            for (int y = 0; y < SIZE; y++) {
                Icon cur = iconfactory.getIconForCell(view[x][y]);
                cells[x][y].setIcon(cur);
                bees[x][y].setIcon(null);
            }
        }
        addBees(env, bee.getPosition(), Bee.VIEW_RANGE);
    }
	private void addBees(Environment env, Point position, int viewRange) {
		for (Point point : env.getVisibleBees(position, viewRange)) {
			bees[Bee.VIEW_RANGE + (point.x - position.x)][Bee.VIEW_RANGE - (point.y - position.y)].setIcon(iconfactory.getIconForBee(bee));
			System.out.println("xx ="+(SIZE/2 + (position.x - point.x) )+ " yy ="+(SIZE/2 + (position.y - point.y)));
		}
	}
}

